<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/main.php';

/**
 * Контроллер контактов
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Contacts extends Main {
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Contacts
    */
   function Contacts() {
      parent::Main();
      $this->load->language('contacts');
      show_404();
   } // end Contacts
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      
      $name = '';
      $email = '';
      $theme = '';
      $content = '';
      
      if (false !== $this->input->post('send')) {
         $name = trim($this->input->post('name'));
         $email = trim($this->input->post('email'));
         $theme = trim($this->input->post('theme'));
         $content = trim($this->input->post('content'));
         
         $this->load->library('validation');
         $rules = array(
            'name'    => 'trim|required',
            'email'   => 'trim|required|valid_email',
            'theme'   => 'trim|required',
            'content' => 'trim|required',
            'captcha' => 'trim|required|callback_check_captcha'
         );
         $this->validation->set_rules($rules);
         if ($this->validation->run()) {
            // Высылаем мыло
            $this->load->library('email');
            $this->email->from($email, $name);
            $this->email->to($this->variables->get('site_email'));
            $this->email->subject($theme);
            $this->email->message($content);
            if ($this->email->send()) {
               $this->set_message($this->lang->line('contacts_sent_succ'), 'info', true);
               $this->redirect($this->get_controller());
            }
            $this->set_message($this->lang->line('contacts_sent_error'), 'error');
         }
      }
      
      // path
      $links = array(
         array(
            'title' => $this->lang->line('front_title'),
            'link'  => base_url()
         ),
         array(
            'title' => $this->lang->line('contacts_title'),
            'link'  => ''
         )
      );
      $this->load->helper('path');
      $this->add_param('path', create_path($links));
      $this->add_param('link', create_link($this->get_controller(), $this->get_method()));
      
      $this->add_param('title', $this->lang->line('contacts_title'));
      
      $data = array(
         'name'    => htmlspecialchars($name),
         'email'   => htmlspecialchars($email),
         'theme'   => htmlspecialchars($theme),
         'content' => htmlspecialchars($content),
         'sid'     => rand(0, 999999)
      );
      $this->set_content($this->parser->parse('contacts/body.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Метод генерирования и отображения каптчи
    *
    * @access public
    */
   function captcha() {
      $this->load->library('captcha');
      $this->captcha->set_width(65);
      $this->captcha->set_height(21);
      $this->captcha->set_length(6);
      $this->captcha->display();
   } // end captcha
   
   /**
    * Функция валидирования каптчи
    *
    * @access public
    * @param string $str
    * @return boolean
    */
   function check_captcha($str) {
      $this->load->library('captcha');
      return $this->captcha->valid_captcha($str);
   } // end check_captcha
   
}
